@isTest
private class EquipmentHandlerTest {
    static testMethod void testMethod1() {
        Account acc = TestUtils.createAccount();
        insert acc;

        CombinationForm__c cf1 = new CombinationForm__c();
        cf1.CombinationForm__c = 'XN-1000';
        cf1.Customer__c = acc.Id;
        CombinationForm__c cf2 = new CombinationForm__c();
        cf2.CombinationForm__c = 'XN-1000';
        cf2.Customer__c = acc.Id;
        insert new CombinationForm__c[] {cf1, cf2};

  //产品结构
        ProductStructure__c ps1=new ProductStructure__c();
        ps1.Name='仪器';
        ps1.Classify1__c='生化';
        ps1.Classify2__c='生化-设备';
        ps1.Classify3__c='仪器';
        insert ps1;
          Brand__c brand = new Brand__c(
            BrandShortName__c = '四川迈克'
        );
        insert brand;
         //产品
        Product__c p1=TestUtils.createProduct();
        p1.Name='仪器';
        p1.Category0__c = '仪器';
        p1.Model__c='Model1228';
        p1.Spec__c = 'xxxxxx';
        p1.ProductStructure__c=ps1.Id;
        p1.CRMBrand__c = brand.Id;
        p1.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
        insert p1;

        Product__c p2=TestUtils.createProduct();
        p2.Name='仪器';
        p2.Category0__c = '仪器';
        p2.Model__c='Model122';
        p2.Spec__c = 'xxxxxx1';
        p2.ProductStructure__c=ps1.Id;
        p2.CRMBrand__c = brand.Id;
        p2.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
        insert p2;

          Product__c p3=TestUtils.createProduct();
        p3.Name='仪器';
        p3.Category0__c = '仪器';
        p3.Model__c='Model123';
        p3.Spec__c = 'xxxxxx3';
        p3.ProductStructure__c=ps1.Id;
        p3.CRMBrand__c = brand.Id;
        p3.RecordTypeId =  Utils.getRecordTypeIdByDevNameAndObjName('NCProduct','Product__c');
        insert p3;

         Dealer__c d = new Dealer__c(
            Only__c='Dealer__c'
        );
        insert d;
        

        InstallationApply__c ins = new InstallationApply__c();
        ins.InstallationType__c='新装机';
        //必填字段 chatter 通知工作流引用到的字段都为必填字段
        ins.Instrument__c = p1.Id;
        ins.NEWDealer__c = d.Id;
        ins.Hospital__c = acc.Id;
        ins.EnterWay__c = '正常销售';
        ins.InstallEstiDate__c = Date.newInstance(2017, 12, 1);
        ins.InstallsNumber__c = 1;
        ins.EffectiveTime__c = System.now();
        insert ins;

        Case cas=new Case();
        cas.AccountId=acc.Id;
        cas.InstallationApply__c = ins.Id;
        insert cas;
        
        WorkOrder__c wo=new WorkOrder__c();
        wo.Case__c=cas.Id;
        wo.Account__c=acc.Id;
        wo.Status__c='处理中';
        wo.ManufactureInfo__c=p1.Id;
        wo.recordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('InstallationOrder','WorkOrder__c');
        wo.WhetherToMakeASingle__c = '否';
        wo.WorkOrderInstallApplication__c = ins.Id;
        insert wo;

        Equipment__c e1 = new Equipment__c();
        e1.Account__c = acc.Id;
        e1.IMEINumber__c = '11';
        e1.CombinationForm__c = cf1.Id;
        e1.RelatedWorkOrders__c =wo.Id;
        e1.Product__c = p1.Id;
        insert e1;

        // 新建一个仪器的情况
        cf1 = [select LinkedEquip__c from CombinationForm__c where Id = :cf1.Id];
       // System.assertEquals('(11)', cf1.LinkedEquip__c);

        Equipment__c e2 = new Equipment__c();
        e2.Account__c = acc.Id;
        e2.IMEINumber__c = '22';
        e2.CombinationForm__c = cf1.Id;
        e2.Product__c = p2.Id;

        Equipment__c e3 = new Equipment__c();
        e3.Account__c = acc.Id;
        e3.IMEINumber__c = '33';
        e3.CombinationForm__c = cf1.Id;
        e3.Product__c = p3.Id;
        insert new Equipment__c[] {e2, e3};

        // 新建多个仪器的情况
        cf1 = [select LinkedEquip__c from CombinationForm__c where Id = :cf1.Id];
      //  System.assertEquals('(11)-(22)-(33)', cf1.LinkedEquip__c);

        e2.CombinationForm__c = cf2.Id;
        update e2;

        // 组合形式变更的情况
        cf1 = [select LinkedEquip__c from CombinationForm__c where Id = :cf1.Id];
       // System.assertEquals('(11)-(33)', cf1.LinkedEquip__c);
        cf2 = [select LinkedEquip__c from CombinationForm__c where Id = :cf2.Id];
       // System.assertEquals('(22)', cf2.LinkedEquip__c);

        e3.IMEINumber__c = '333';
        update e3;


        // 机身号变更的情况
        cf1 = [select LinkedEquip__c from CombinationForm__c where Id = :cf1.Id];
        //System.assertEquals('(11)-(333)', cf1.LinkedEquip__c);
    }
}